<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:29
 */

class OnlineService extends Service {
	public static function intervalHeader($params){
		$header = array(
				'ctext'         => array('text' => '在线时长(分钟)','width' => '230px'),
				'i_0_30'        => array('text' => '[0,30]', 'rule' => 'numeric'),
				'i_31_60'       => array('text' => '[31,60]', 'rule' => 'numeric'),
				'i_61_120'      => array('text' => '[61,120]', 'rule' => 'numeric'),
				'i_121_240'     => array('text' => '[121,240]', 'rule' => 'numeric'),
				'i_241_360'     => array('text' => '[241,360]', 'rule' => 'numeric'),
				'i_361'         => array('text' => '[361,+]', 'rule' => 'numeric'),
		);
		return $header;
	}
	
	public static function intervalData($params){
		if(PubFunc::checkEmpty($params, array('g', 'server'))) return array();
	
	    $modOnline = new ModOnline();
	
		$data = $modOnline->getIntervalData($params);
	
		$data_d = array();
		foreach ($data as $key=>$val){
			$data_d[$val['RecDate']][] = $val;
		}
	
		$result = array();
		foreach ($data_d as $key=>$val){
			$RMB_0_30 = $RMB_31_60 = $RMB_61_120 = $RMB_121_240 = $RMB_241_360 = $RMB_361 = 0;
			$poor_0_30 = $poor_31_60 = $poor_61_120 = $poor_121_240 = $poor_241_360 = $poor_361 = 0;
			$user_0_30 = $user_31_60 = $user_61_120 = $user_121_240 = $user_241_360 = $user_361 = 0;
			$all_online = 0; //总在线时长
			$all_user = 0;   //总在线人数
			

			foreach ($val as $k=>$v){
				$all_online += $v['OnlineTime'];
				$all_user += $v['OnlineUser'];
				if($v['OnlineTime'] >= 0 && $v['OnlineTime'] <= 30*60){
					$user_0_30 += $v['OnlineUser'];
					if($v['IsRMBPlayer'] == 1){
						$RMB_0_30 += $v['OnlineUser'];
					}
				    if($v['IsRMBPlayer'] == 0){
						$poor_0_30 += $v['OnlineUser'];
					}
				}
				
				if($v['OnlineTime'] >= 30*60+1 && $v['OnlineTime'] <= 60*60){
					$user_31_60 += $v['OnlineUser'];
					if($v['IsRMBPlayer'] == 1){
						$RMB_31_60 += $v['OnlineUser'];
					}
					if($v['IsRMBPlayer'] == 0){
						$poor_31_60 += $v['OnlineUser'];
					}				
				}
				
				if($v['OnlineTime'] >= 60*60+1 && $v['OnlineTime'] <= 120*60){
					$user_61_120 += $v['OnlineUser'];
					if($v['IsRMBPlayer'] == 1){
						$RMB_61_120 += $v['OnlineUser'];
					}
					if($v['IsRMBPlayer'] == 0){
						$poor_61_120 += $v['OnlineUser'];
					}
				}
				
				if($v['OnlineTime'] >= 120*60+1 && $v['OnlineTime'] <= 240*60){
					$user_121_240 += $v['OnlineUser'];
					if($v['IsRMBPlayer'] == 1){
						$RMB_121_240 += $v['OnlineUser'];
					}
					if($v['IsRMBPlayer'] == 0){
						$poor_121_240 += $v['OnlineUser'];
					}
				}
				
				if($v['OnlineTime'] >= 240*60+1 && $v['OnlineTime'] <= 360*60){
					$user_241_360 += $v['OnlineUser'];
					if($v['IsRMBPlayer'] == 1){
						$RMB_241_360 += $v['OnlineUser'];
					}
					if($v['IsRMBPlayer'] == 0){
						$poor_241_360 += $v['OnlineUser'];
					}
				}
				
				if($v['OnlineTime'] >= 360*60+1){
					$user_361 += $v['OnlineUser'];
					if($v['IsRMBPlayer'] == 1){
						$RMB_361 += $v['OnlineUser'];
					}
					if($v['IsRMBPlayer'] == 0){
						$poor_361 += $v['OnlineUser'];
					}
				}
			}
			 
			$RMB_rate_0_30 = number_format($RMB_0_30 / $user_0_30 * 100, 2).'%';
			$poor_rate_0_30 = number_format($poor_0_30 / $user_0_30 * 100, 2).'%';
			$RMB_rate_31_60 = number_format($RMB_31_60 / $user_31_60 * 100, 2).'%';
			$poor_rate_31_60 = number_format($poor_31_60 / $user_31_60 * 100, 2).'%';
			$RMB_rate_61_120 = number_format($RMB_61_120 / $user_61_120 * 100, 2).'%';
			$poor_rate_61_120 = number_format($poor_61_120 / $user_61_120 * 100, 2).'%';
			$RMB_rate_121_240 = number_format($RMB_121_240 / $user_121_240 * 100, 2).'%';
			$poor_rate_121_240 = number_format($poor_121_240 / $user_121_240 * 100, 2).'%';
			$RMB_rate_241_360 = number_format($RMB_241_360 / $user_241_360 * 100, 2).'%';
			$poor_rate_241_360 = number_format($poor_241_360 / $user_241_360 * 100, 2).'%';
			$RMB_rate_361 = number_format($RMB_361 / $user_361 * 100, 2).'%';
			$poor_rate_361 = number_format($poor_361 / $user_361 * 100, 2).'%';
			
			$avg_online = number_format($all_online / ($all_user*60), 2);
			
			 
			$data_n[0] = array('ctext' => 'RMB玩家','i_0_30'=>$RMB_0_30 .' ('.$RMB_rate_0_30.')','i_31_60'=>$RMB_31_60 .' ('.$RMB_rate_31_60.')',
					'i_61_120'=>$RMB_61_120 .' ('.$RMB_rate_61_120.')','i_121_240'=>$RMB_121_240 .' ('.$RMB_rate_121_240.')',
					'i_241_360'=>$RMB_241_360 .' ('.$RMB_rate_241_360.')','i_361'=>$RMB_361 .' ('.$RMB_rate_361.')');
			$data_n[1] = array('ctext' => '非RMB玩家','i_0_30'=>$poor_0_30 .' ('.$poor_rate_0_30.')','i_31_60'=>$poor_31_60 .' ('.$poor_rate_31_60.')',
					'i_61_120'=>$poor_61_120 .' ('.$poor_rate_61_120.')','i_121_240'=>$poor_121_240 .' ('.$poor_rate_121_240.')',
					'i_241_360'=>$poor_241_360 .' ('.$poor_rate_241_360.')','i_361'=>$poor_361 .' ('.$poor_rate_361.')');
			
			$user_rate_0_30 = number_format($user_0_30 / $all_user * 100, 2).'%';
			$user_rate_31_60 = number_format($user_31_60 / $all_user * 100, 2).'%';
			$user_rate_61_120 = number_format($user_61_120 / $all_user * 100, 2).'%';
			$user_rate_121_240 = number_format($user_121_240 / $all_user * 100, 2).'%';
			$user_rate_241_360 = number_format($user_241_360 / $all_user * 100, 2).'%';
			$user_rate_361 = number_format($user_361 / $all_user * 100, 2).'%';
			$data_n[2] = array('ctext' => '总数 &nbsp;<font color="red">[平均在线时长]:'.$avg_online.'分钟 &nbsp;&nbsp;&nbsp;[总人数]:'.$all_user.'</font>',
					'i_0_30'=>$user_0_30.' ('.$user_rate_0_30.')','i_31_60'=>$user_31_60.' ('.$user_rate_31_60.')','i_61_120'=>$user_61_120.' ('.$user_rate_61_120.')',
					'i_121_240'=>$user_121_240.' ('.$user_rate_121_240.')','i_241_360'=>$user_241_360.' ('.$user_rate_241_360.')','i_361'=>$user_361.' ('.$user_rate_361.')');
			$result[$key] = $data_n;
		}
	
		$begin  = strtotime($params['time_start']);
		$end    = strtotime($params['time_end']) + 86400;
		for(;$begin<$end;$begin+=86400){
			$day = date('Y-m-d', $begin);
			if(!$result[$day]){
				$data_n[0] = array('ctext' => 'RMB玩家','i_0_30'=>0,'i_31_60'=>0,'i_61_120'=>0,'i_121_240'=>0,'i_241_360'=>0,'i_361'=>0);
				$data_n[1] = array('ctext' => '非RMB玩家','i_0_30'=>0,'i_31_60'=>0,'i_61_120'=>0,'i_121_240'=>0,'i_241_360'=>0,'i_361'=>0);
				$data_n[2] = array('ctext' => '总数','i_0_30'=>0,'i_31_60'=>0,'i_61_120'=>0,'i_121_240'=>0,'i_241_360'=>0,'i_361'=>0);				
				$result[$day]= $data_n;
			}
		}
		krsort($result);
		return $result;
	}
	
	/**
	 * 以下方法暂废弃使用
	 */

    public static function indexHeader(){
        $header = array(
            'RecDate'       => array('text' => PubFunc::t('日期')),
            'SID'           => array('text' => PubFunc::t('服')),
            'TotalUser'     => array('text' => '总人数'),
            'LogoutUser'    => array('text' => '登出人数'),
            'Ka'    => array('text' => PubFunc::t('在线时长人数'), 'include' => array(
                "a0"    => array("text" => "0~4分钟"),
                "a5"    => array("text" => "5~9分钟"),
                "a10"    => array("text" => "5~9分钟"),
                "a15"    => array("text" => "15~29分钟"),
                "a30"    => array("text" => "30~59分钟"),
                "a60"    => array("text" => "1~2小时"),
                "a120"    => array("text" => "2~3小时"),
                "a180"    => array("text" => "3~4小时"),
                "a240"    => array("text" => "4~6小时"),
                "a360"    => array("text" => "6~8小时"),
                "a480"    => array("text" => "8~10小时"),
                "a600"    => array("text" => "10~12小时"),
                "a720"    => array("text" => "12小时以上"),
            ))
        );

        return $header;
    }

    public static function indexData($params){
        $data = ModOnline::indexData($params);
        return $data;
    }

    public static function indexChart($data){
        $chartData = array();

        $keys = array(
            "a0"    => "0~4分钟",
            "a5"    => "5~9分钟",
            "a10"   => "5~9分钟",
            "a15"   => "15~29分钟",
            "a30"   => "30~59分钟",
            "a60"   => "1~2小时",
            "a120"  => "2~3小时",
            "a180"  => "3~4小时",
            "a240"  => "4~6小时",
            "a360"  => "6~8小时",
            "a480"  => "8~10小时",
            "a600"  => "10~12小时",
            "a720"  => "12小时以上",
        );

        foreach($data as $v){
            foreach($keys as $kk => $vv){
                $chartData[$v['RecDate']][$vv] = $v[$kk];
            }
        }
        return array(
            "data"=> $chartData
        );
    }
} 